using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UKVSTS.WorkItemControls
{
    public partial class Edit : Form
    {
        private String m_DestDir;
        
        public Edit(Image img, String destDir)
        {
            InitializeComponent();

            m_DestDir = destDir;

            imgEditCtrl.Image = img;
            imgEditCtrl.Mode = SimpleImageEditControl.SimpleImageEditSurface.Mode.Cropping;
        }

        private void Edit_Load(object sender, EventArgs e)
        {

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (DialogResult == DialogResult.Cancel)
            {
                DialogResult res = MessageBox.Show(this,
                                                    "The Image has been changed, are your sure you want to discard these changes?",
                                                    "Warning",
                                                    MessageBoxButtons.YesNo,
                                                    MessageBoxIcon.Warning,
                                                    MessageBoxDefaultButton.Button2);

                if (res == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
            else
            {
                bool invalidName = String.IsNullOrEmpty(txtFileName.Text);

                if (txtFileName.Text.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
                {
                    invalidName = true;
                    return;
                }

                if (invalidName)
                {
                    DialogResult res = MessageBox.Show(this,
                                                        "The file name must have a valid name.\n",
                                                        "Warning",
                                                        MessageBoxButtons.OK,
                                                        MessageBoxIcon.Warning);
                    e.Cancel = true;
                    return;
                }

                String fileName = Path.Combine(m_DestDir, txtFileName.Text);
                if (File.Exists(fileName))
                {
                    DialogResult res = MessageBox.Show(this,
                                                        "The file name must be unique.\n",
                                                        "Warning",
                                                        MessageBoxButtons.OK,
                                                        MessageBoxIcon.Warning);
                    e.Cancel = true;
                    return;
                }

            }
        }

        public Image GetModifiedImage()
        {
            return imgEditCtrl.GetModifiedImage();
        }

        public String FileName
        {
            get { return txtFileName.Text; }
            set
            {
                txtFileName.Text = value;
            }
        }

        public String Comment
        {
            get { return txtComment.Text; }
            set
            {
                txtComment.Text = value;
            }
        }

    }
}